﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ppt = Microsoft.Office.Interop.PowerPoint;

namespace ObjectModel.PowerPoint
{
    /// <summary>
    /// A PowerPoint TextRange object that can resolve itself from serializable info.
    /// </summary>
    public class ResolvableTextRange : ResolvableObject
    {
        public ResolvableShape Shape { get; set; }

        public int Start { get; set; }

        public int Length { get; set; }

        public ResolvableTextRange() { } // Parameterless constructor required for serialization.
        
        public ResolvableTextRange(Ppt.TextRange textRange)
        {
            Shape = new ResolvableShape(textRange.GetParent<Ppt.Shape>()); ;
            Start = textRange.Start;
            Length = textRange.Length;
        }

        public override object Resolve()
        {
            Ppt.Shape shape = Shape.Resolve() as Ppt.Shape;
            return shape.TextFrame.TextRange.Characters(Start, Length);
        }

        public bool Equals(Ppt.TextRange textRange)
        {
            Ppt.Shape shape = Shape.Resolve() as Ppt.Shape;
            return shape == textRange.GetParent<Ppt.Shape>() &&
                Start == textRange.Start &&
                Length == textRange.Length;
        }
    }
}
